<?php
/**
 * Created by PhpStorm.
 * User: zhugeng
 * Date: 2019/4/22
 * Time: 10:31
 */

namespace app\api\controller;

use app\common\service\system\SystemUser;

use think\Controller;
use think\Db;
use think\Session;

header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept');

class Login extends Controller
{
    /**
     * 前端登录
     * @author JiYun On 2019/05/09
     * @return \think\response\Json
     */
    public function login()
    {
        $param = $this->request->param();
        $ip    = $this->request->ip();
        
        $user   = new SystemUser();
        $result = $user->login($param['username'], $param['password'], $ip);
        
        if ($result !== null) {
            return json($result, 200);
        } else {
            return json('', 403);
        }
    }
    
    /**
     * 前端退出
     * @author JiYun On 2019/05/09
     */
    public function logout()
    {
        $param = $this->request->param();

        $user   = new SystemUser();
        $result = $user->logout($param['token']);

        if ($result === true) {
            return json('', 200);
        } else {
            return json('', 500);
        }
    }

    /**
     * 首页天气状况
     * @author ayy 2019/07/11
     */
    public function weather()
    {
        $date = date('Y-m-d H').':00:00';

        //获取天气
        $weather = Db::table('air_quality_weather_realtime_hour')
            ->where(['region_id'=>53,'publish_time'=>$date])
            ->field('surface_temp,sky_condition_icon,sky_condition')
            ->find();
        if (empty($weather)) {
            $weather = [
                'surface_temp' => '0',
                'sky_condition_icon' => '',
                'sky_condition' => ''
            ];
        }

        //获取PM2.5
        $pm25 = Db::table('air_quality_station_aqi_realtime_hour')
            ->where(['region_id'=>53,'station_id'=>0,'publish_time'=>$date])
            ->field('pm25,aqi,aqi_level')
            ->find();
        if (empty($pm25)) {
            $pm25 = [
                'pm25' => '0',
                'aqi' => '0',
                'aqi_level' => ''
            ];
        }

        $data = array_merge($weather,$pm25);
        $data['city'] = '长治';
        $data['date'] = date('m').'月'.date('d').'日';
        $data['week'] = get_week();
        return json($data,'200');
    }
}
